package com.boatAnchorAssistant.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boatAnchorAssistant.pojo.entity.AlarmCause;
import com.boatAnchorAssistant.pojo.entity.AlarmType;
import com.boatAnchorAssistant.pojo.entity.EventGroup;
import com.boatAnchorAssistant.pojo.param.alarmCause.*;
import com.boatAnchorAssistant.pojo.vo.alarmCause.*;
import com.boatAnchorAssistant.pojo.dto.alarmCause.*;
import com.boatAnchorAssistant.service.check.ClassUtil;
import com.boatAnchorAssistant.service.repository.in.AlarmCauseRepository;
import com.boatAnchorAssistant.service.repository.in.AlarmTypeRepository;
import com.boatAnchorAssistant.service.service.in.AlarmCauseService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.exceptions.BizException;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【描 述】：报警原因服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class AlarmCauseServiceImpl implements AlarmCauseService {

    private final AlarmCauseRepository alarmCauseRepository;
    private final AlarmTypeRepository alarmTypeRepository;
    private final ClassUtil<AlarmCause> classUtil;
    public AlarmCauseServiceImpl(AlarmCauseRepository alarmCauseRepository, AlarmTypeRepository alarmTypeRepository, ClassUtil<AlarmCause> classUtil) {
        this.alarmCauseRepository = alarmCauseRepository;
        this.alarmTypeRepository = alarmTypeRepository;
        this.classUtil = classUtil;
    }

    void check(Boolean isSave,AlarmCause entity){
        try {
            if (isSave) {
                classUtil.addIsHave(entity, alarmCauseRepository);
            } else {
                classUtil.updateIsHave(entity, alarmCauseRepository,entity.getId());
            }
        }
        catch (Exception e){
            e.printStackTrace();
            throw new BizException(e.getMessage());
        }
    }

    /**
     * 创建报警原因
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createAlarmCause(CreateAlarmCauseParam param) {
        AlarmCause alarmCause = new AlarmCause();
        BeanUtils.copyProperties(param, alarmCause);
        check(true,alarmCause);
        Long id = IdGenerator.SNOW.generate();
        alarmCause.setId(id);
        alarmCauseRepository.create(alarmCause);
    }

    /**
     * 修改报警原因
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editAlarmCause(EditAlarmCauseParam param) {
        Long id = param.getId();
        AlarmCause alarmCause = alarmCauseRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, alarmCause);
        check(false,alarmCause);
        alarmCauseRepository.edit(alarmCause);
    }

    /**
     * 移除报警原因
     *
     * @param id
     */
    @Override
    public void removeAlarmCause(Long id) {
        alarmCauseRepository.removeById(id);
    }

    /**
     * 启动/禁用报警原因
     *
     * @param id
     */
    @Override
    public void enabledAlarmCause(Long id) {
        AlarmCause alarmCause = alarmCauseRepository.getByIdCheck(id);
        alarmCause.setIsDisable(!alarmCause.getIsDisable());
        alarmCauseRepository.edit(alarmCause);
    }

    /**
     * 获取报警原因详情
     *
     * @param id
     * @return
     */
    @Override
    public GetAlarmCauseVo getAlarmCause(Long id) {
        AlarmCause alarmCause = alarmCauseRepository.getByIdCheck(id);
        GetAlarmCauseVo vo = new GetAlarmCauseVo();
        BeanUtils.copyProperties(alarmCause, vo);
        return vo;
    }

    /**
     * 获取报警原因 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetAlarmCauseListVo> getAlarmCauseList(GetAlarmCauseListParam param) {

        SelectAlarmCauseDto dto = new SelectAlarmCauseDto();
        BeanUtils.copyProperties(param, dto);
        Page<AlarmCause> page = alarmCauseRepository.selectAlarmCause(dto);
        List<AlarmCause> alarmCauseList = page.getRecords();
        List<GetAlarmCauseListVo> list = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(alarmCauseList)) {
            List<Long> ids = alarmCauseList.stream().map(AlarmCause::getTypeId).collect(Collectors.toList());
            Map<Long, AlarmType> map = alarmTypeRepository.getMap(ids);
            for (AlarmCause alarmCause : alarmCauseList) {
                GetAlarmCauseListVo vo = new GetAlarmCauseListVo();
                BeanUtils.copyProperties(alarmCause, vo);
                if (map != null) {
                    AlarmType type = map.get(alarmCause.getTypeId());
                    if (type != null) {
                        vo.setTypeName(type.getName());
                    }
                }
                list.add(vo);
            }
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取报警原因下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetAlarmCauseDownListVo> getAlarmCauseDownList(GetAlarmCauseDownListParam param) {
        AlarmCause po = new AlarmCause();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<AlarmCause> alarmCauseList = alarmCauseRepository.getListByEntity(po);

        List<GetAlarmCauseDownListVo> list = new ArrayList<>();
        for (AlarmCause alarmCause : alarmCauseList) {
            GetAlarmCauseDownListVo vo = new GetAlarmCauseDownListVo();
            BeanUtils.copyProperties(alarmCause, vo);
            list.add(vo);
        }
        return list;
    }


}



